package com.baoming.util;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.UUID;

import javax.imageio.ImageIO;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class Struts2UploadUtil {
	
	public static boolean fileUtil(java.io.File file){
		if(file != null && !"".equals(file.getName().toString())){
			return true ; 
		}else{
			return false ; 
		}
	}
	
	public static String getSmallPic(String bigPic){
		int index = bigPic.lastIndexOf(".") ; 
		String subBigPic = bigPic.substring(0, index) ; 
		String lastStr = bigPic.substring(index, bigPic.length()) ;  
		return subBigPic+"_small"+lastStr ; 
	}
 
	/** 
	 * 
	 * @param oldFile
	 * @param width
	 * @param height
	 * @param quality
	 * @param smallIcon
	 * @param percentage
	 * @return
	 */
	public static String doCompress(String oldRealPath, int width, int height,float quality, String smallIcon, boolean percentage) {
		
		if (oldRealPath != null && width > 0 && height > 0) {
			
			String realPath = oldRealPath ;  
			
			 Image srcFile=null;
			 String newImage = null;
			 File oldFile2 = new File(realPath) ; 
			 if(!oldFile2.exists()){
				 return null ; 
			 }
			 try {
				srcFile = ImageIO.read(oldFile2);
				 int new_w = width;
			     int new_h = height;
			     
			     if (percentage) { 
			         double rate1 = ((double) srcFile.getWidth(null)) / (double) width + 0.1;
			         double rate2 = ((double) srcFile.getHeight(null)) / (double) height + 0.1;
			         double rate = rate1 > rate2 ? rate1 : rate2;
			         new_w = (int) (((double) srcFile.getWidth(null)) / rate);
			         new_h = (int) (((double) srcFile.getHeight(null)) / rate);
			     }
			     
			     BufferedImage tag = new BufferedImage(new_w, new_h, BufferedImage.TYPE_INT_RGB);
			     tag.getGraphics().drawImage(srcFile, 0, 0, new_w, new_h, null);
			     
			     String filePrex = realPath.substring(0, realPath.lastIndexOf('.'));
			     newImage = filePrex +  smallIcon + realPath.substring(filePrex.length()); 
			      
			     FileOutputStream out = new FileOutputStream(newImage);
			     JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
			     JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag);
			      
			     jep.setQuality(quality, true);
			     encoder.encode(tag, jep);
			     out.close();
				
			} catch (IOException e) { 
				e.printStackTrace();
			}finally{
			     srcFile.flush();
		    }
			return newImage ; 
		}
		return null ;
	}

	public static String getNewFileName(String oldName) {
		int beginIndex = oldName.lastIndexOf(".");
		String newFileName = UUID.randomUUID().toString()
				+ oldName.substring(beginIndex, oldName.length());
		return newFileName;
	}

	public static String uploadSimple(File file, String fileName,
			String saveFilePath) {

		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		String dateString = formatter.format(new java.util.Date());

		String realpath = ServletActionContext.getServletContext().getRealPath(
				saveFilePath);
		File saveFile = new File(realpath);

		if (!saveFile.exists()) {
			saveFile.mkdir();
		}

		String newFileName = getNewFileName(fileName);
		String newFile = realpath + File.separator + dateString
				+ File.separator + newFileName;

		File file1 = new File(newFile);

		try {
			FileUtils.copyFile(file, file1);
		} catch (IOException e) {
			e.printStackTrace();
			return "";
		}
		String responsePath = saveFilePath + "/" + dateString + "/"+ newFileName;
		  
		return responsePath;
	}
 

}
